package com.cloudxingkong.dianyinke.VideoPlay;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.android.tedcoder.wkvideoplayer.dlna.engine.DLNAContainer;
import com.android.tedcoder.wkvideoplayer.dlna.service.DLNAService;
import com.android.tedcoder.wkvideoplayer.model.Video;
import com.android.tedcoder.wkvideoplayer.model.VideoUrl;
import com.android.tedcoder.wkvideoplayer.util.DensityUtil;
import com.android.tedcoder.wkvideoplayer.view.SuperVideoPlayer;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.LoginJsonUtils;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.UploadUtils;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;

import org.xutils.http.RequestParams;

import java.util.ArrayList;

public class SuperVideoPlay extends AppCompatActivity implements SuperVideoPlayer.VideoPlayCallbackImpl {

    private SuperVideoPlayer mSuperVideoPlayer;
    private String videoPath;
    private String videoID;
    private SharedPreferences sharedPreferences;
    private String sessionID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_super_video_play);
        mSuperVideoPlayer = (SuperVideoPlayer) findViewById(R.id.video_player);
        mSuperVideoPlayer.setVideoPlayCallback(this);
        Intent intent = getIntent();
        videoPath = intent.getStringExtra("videoPath");
        videoID = intent.getStringExtra("videoID");
        videoViewsUp();
        startDLNAService();
        loadVideo();
        sharedPreferences = getSharedPreferences("data_dyk",0);
        sessionID = CookieHelper.getSessionID(sharedPreferences);

    }

    private void videoViewsUp() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.VIDEO_VIEWS_UP);
                params.addHeader("cookie", sessionID);
                params.addBodyParameter("id", videoID);
                UploadUtils.postData(params, new UploadUtils.UploadCallback() {
                    @Override
                    public void onFinish(String state) {
                        Log.e("test", "视频播放次数增加  " + state);
                    }
                });
            }
        }.start();
    }

    /**
     * 加载视频
     */
    private void loadVideo() {
        mSuperVideoPlayer.setAutoHideController(false);
        Video video = new Video();
        VideoUrl videoUrl = new VideoUrl();
        videoUrl.setFormatName("normal");
        videoUrl.setFormatUrl(videoPath);
        ArrayList<VideoUrl> arrayUrl = new ArrayList<>();
        arrayUrl.add(videoUrl);
        video.setVideoName("视频");
        video.setVideoUrl(arrayUrl);

        ArrayList<Video> arrayVideo = new ArrayList<>();
        arrayVideo.add(video);

        mSuperVideoPlayer.loadMultipleVideo(arrayVideo, 0, 0, 0);


    }

    /**
     * 开启服务
     */
    private void startDLNAService() {
        DLNAContainer.getInstance().clear();
        Intent intent = new Intent(getApplicationContext(), DLNAService.class);
        startService(intent);
    }

    /**
     * 按下返回键回调处理
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK) {
            mSuperVideoPlayer.close();
            setResult(10086);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 屏幕旋转后的回调
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mSuperVideoPlayer == null) {
            return;
        }
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().getDecorView().invalidate();
            float height = DensityUtil.getWidthInPx(this);
            float width = DensityUtil.getHeightInPx(this);
            mSuperVideoPlayer.getLayoutParams().height = (int) width;
            mSuperVideoPlayer.getLayoutParams().width = (int) height;
        }
    }

    /**
     * 关闭videoplayer回调
     */
    @Override
    public void onCloseVideo() {
        mSuperVideoPlayer.close();
    }

    @Override
    public void onSwitchPageType() {

    }

    @Override
    public void onPlayFinish() {

    }

    @Override
    public void onClickLikeInSVP(FrameLayout layout) {
        new Thread(){
            @Override
            public void run() {
                super.run();
                RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.VIDEO_LIKE_UP);
                params.addHeader("cookie",sessionID);
                params.addBodyParameter("id", videoID);
                UploadUtils.postData(params, new UploadUtils.UploadCallback() {
                    @Override
                    public void onFinish(String state) {
                        boolean isSucc = false;
                        Log.e("test", "视频点赞 " + state);
                        Object success = LoginJsonUtils.getJsonFromLogin(state).get("success");
                        isSucc = (boolean) success;
                        if (isSucc)
                            Toast.makeText(SuperVideoPlay.this, "赞成功", Toast.LENGTH_SHORT).show();
                        else
                            Toast.makeText(SuperVideoPlay.this, "赞失败,请稍后重试~", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();
    }

    /**
     * 销毁后停止服务
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopDLNAService();
        mSuperVideoPlayer.close();
    }

    /**
     * 关闭服务
     */
    private void stopDLNAService() {
        Intent intent = new Intent(getApplicationContext(), DLNAService.class);
        stopService(intent);
    }


}
